<template>
  <div class="app-container">
    <div style="text-align:right;margin-top: -45px;margin-bottom: 5px;">
      <el-badge :value="value" class="item">
        <el-button size="small" @click="handleCar"><i class="el-icon-shopping-cart-2"></i>购物车</el-button>
      </el-badge>
    </div>
    <div>
      <el-row :gutter="20">
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <!-- <menu></menu> -->
            <el-menu default-active="0" @select="handleSelect" class="el-menu-vertical-demo" background-color="#f9ca24"
              text-color="#3d3c3c" active-text-color="#3d3c3c">
              <el-menu-item :index="index" v-for="(item, index) in categoryList">
                <span slot="title">
                  <div style="margin-left: 15px;" @click.active="detail(item)">{{ item.name }}</div>
                </span>
              </el-menu-item>
            </el-menu>
          </div>
        </el-col>
        <el-col :span="20">
          <mainInfo :furitList="fruitList" :detailId="detailId" @sendValue="sendValue" @flushFruit="flush" />
        </el-col>
      </el-row>
    </div>
    <el-dialog title="我的购物车" :visible.sync="dialogVisible" width="30%">
      <div class="block">
        <el-timeline>
          <el-timeline-item :timestamp="item.goods.name" placement="top" v-for="item in fruitListCar">
            <el-card>
              <div style="display: flex;">
                <div style="flex: 1;">
                  <el-image style="width: 70px; height: 70px" :src="item.goods.image"></el-image>
                </div>
                <div>
                  <div style="margin-top: 10px;">
                    <el-input-number size="mini" v-model="item.number" @change="handleChange(item, $event)" :min="0"
                      :max="1000" label="描述文字"></el-input-number>
                  </div>
                  <p>价格：{{ item.number * item.goods.price }}</p>
                </div>
              </div>
            </el-card>
          </el-timeline-item>
        </el-timeline>
        <div>
          总价：{{ totalPrice }}
        </div>
        <div style="float: right;">
          <el-button type="primary" size="mini" @click="handleOrder">确认下单</el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import mainInfo from "../dashboard/component/mainInfo.vue"
import { getCategoryByPage } from "@/api/category.js"
import { getByCategory } from "@/api/fruit.js"
import { getMyCar, addCar } from "@/api/car.js"
import { addOrder } from "@/api/order.js"
export default {
  data() {
    return {
      categoryList: [],
      fruitList: [],
      getFormDate: {},
      value: 0,
      dialogVisible: false,
      fruitListCar: [],
      price: 0,
      detailId: ""
    }
  },
  created() {
    this.initCategory();
    this.getMyCarList()
  },
  computed: {
    totalPrice() {
      console.log("this.fruitListCar", this.fruitListCar);
      
      let totalPrice = 0
      for (let i of this.fruitListCar) {
        totalPrice += parseFloat(i.goods.price) * (i.number);
      }
      this.price = totalPrice
      return totalPrice
    }
  },
  components: {
    mainInfo
  },
  methods: {
    handleSelect(index, indexPath) {
      console.log("value", index);
    },
    getUsers() { },
    handleOrder() {
      addOrder({ totalPrice: this.price, status: 1, list: this.fruitListCar }).then(res => {
        this.getMyCarList()
      })
    },
    handleChange(item, currentValue) {
      let senddata = {
        fruitId: item.fruit.id,
        number: currentValue
      }
      addCar(senddata).then(res => {
        // this.initCar()
        this.getMyCarList()
      })
    },
    detail(row) {
      this.detailId = row.id
      getByCategory(row.id).then(res => {
        if (res.data.code == 200) {
          this.fruitList = res.data.data
        }
      })
    },
    flush(value) {
      getByCategory(value).then(res => {
        if (res.data.code == 200) {
          this.fruitList = res.data.data
        }
      })
    },
    initCategory() {
      getCategoryByPage({ page: 1, size: 100 }).then(res => {
        if (res.data.code == 200) {
          this.categoryList = res.data.data.records
          getByCategory(this.categoryList[0].id).then(res => {
            if (res.data.code == 200) {
              this.fruitList = res.data.data
            }
          })
        }
      })
    },
    sendValue(value) {
      this.value = 0
      console.log("value", value);
      this.fruitListCar = value
      for (let i of this.fruitListCar) {
        this.value += i.number;
      }

    },
    handleCar() {
      this.dialogVisible = true
    },
    getMyCarList() {
      this.value = 0
      getMyCar().then(res => {
        this.fruitListCar = res.data.data
        for (let i of this.fruitListCar) {
          this.value += i.number;
        }
      })
    }
  }
}
</script>

<style>
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>